package com.varian.tool.result;

import cn.hutool.core.map.MapUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.varian.tool.contant.ResultConstant;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Map;

/**
 * @author ben
 * @since 2024/6/22
 */
@Schema(title = "通用返回体")
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonInclude
@JsonDeserialize(as = R.class)
public interface IR {

    @Schema(title = "状态码")
    Integer getStatus();

    @Schema(title = "异常代码")
    String getCode();

    @Schema(title = "承载数据")
    Object getData();

    @Schema(title = "异常信息")
    String getMessage();

    @Schema(title = "是否成功")
    default boolean isSuccess() {
        return ResultConstant.OK.status().equals(getStatus());
    }

    default Map<String, Object> toMap() {
        return MapUtil.<String, Object>builder()
                .put(ResultConstant.R_STATUS, getStatus())
                .put(ResultConstant.R_CODE, getCode())
                .put(ResultConstant.R_MESSAGE, getMessage())
                .put(ResultConstant.R_DATA, getData())
                .put(ResultConstant.R_SUCCESS, isSuccess())
                .build();
    }
}
